home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6857 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.2 KB  |  192 lines

  1. Path: qualcomm.com!usenet
  2. From: nabbasi@qualcomm.com (Nasser Abbasi)
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: on OO differnces between Ada95 and C++
  5. Date: 20 Feb 1996 06:37:46 GMT
  6. Organization: QUalcomm Inc.
  7. Message-ID: <4gbq7q$g08@qualcomm.com>
  8. Reply-To: x!news.be.innet.net!INbe.net!usenet
  9. NNTP-Posting-Host: annex-p15.qualcomm.com
  10. X-Newsreader: WinVN 0.90.4
  11.  
  12.  
  13.  
  14. Hello,
  15.  
  16. I have a simple comment, but probably a long way of 
  17. showing it :)
  18.  
  19. I have been playing around with the OO features in Ada95 and
  20. comparing it with C++. I noticed this little difference, and I'd
  21. like to see what you think of it.
  22.  
  23. Lets assume we have a base class called Account, and a class called
  24. Saving_Account that uses Account as base. 
  25.  
  26. Lets also assume that we need to define a Money Type, defined in
  27. the base class Account.     
  28.  
  29. In Ada95 this type is defined in the package Account.ads that also
  30. includes the definition of the tagged record type Account (along with
  31. operations that act on Account type).
  32.  
  33. In C++, this Money Type is typedef'ed inside the public part of the
  34. class Account, and it becomes part of the public interface of the
  35. base class.
  36.  
  37. so far so good.
  38.  
  39. Now, In Ada95, a client that wishes to use Saving_Account type (and
  40. any operations on it) will "with" the Saving_Account Package.
  41.  
  42. Also, in C++, a client who wishes to use Saving_Account class will
  43. include "saving_account.h"
  44.  
  45. There is some differences though.
  46.  
  47. In C++, the client to the saving_account class can also use the
  48. Money_Type type (even though that is defined in
  49. the base class Account) without having to include base class
  50. "account.h", this is because Money_Type has become a public part of the
  51. Saving_Account class when Saving_Account inherited Saving class.
  52.  
  53. In Ada95, the client of Saving_Account has no viability to Money_Type
  54. type definition even though they with'ed Saving_account package, since
  55. Money_Type is not a "inherited" by the Saving_Account package
  56. from the Account package. This means that in Ada95, If one wants to
  57. access things like type definitions that are not tagged, but 
  58. used in defining components inside the tagged record, one must
  59. "with" the client package and also packages that the client package
  60. with'ed just to be able to have viability to those type definitions.
  61.  
  62. I understand the reason why in Ada95 one must do this,
  63. a client "with"ing a package has only viability to the public part
  64. of that package and not to any packages that that package may have
  65. "with"ed. 
  66.  
  67. In this case, It seems the C++ way of having everything inside
  68. the public base class becoming visible to clients of the derived class
  69. that inherits the base class is more economical?. 
  70. (everything means, data members, methods members and things like 
  71. enum, typedefs, structs. etc..) In Ada95 only certain type 
  72. (the tagged record type) and methods on this type can be seen 
  73. by clients of the extended type.
  74.  
  75.  
  76. One way to go around having in Ada95 to "with" both Account and
  77. Saving_Account packages, is that in the Saving_Account package 
  78. to introduce new type Money_Type made of the Account.Money_Type,
  79. this way clients of Saving_Account can see Money_Type type by just
  80. "with"ing Saving_Account package and not have to "with" account
  81. package also just to use the Money_Type. 
  82.  
  83. But this means that type definitions has to be manually "propagated" from
  84. one package to another so that clients of the last package in the chain
  85. have only to "with" that last package. Any better way?
  86.  
  87. Just to put all this in example, (In case the above got to winded :) , 
  88. I have an example to show I mean. I show the Ada code, and below it the 
  89. C++ code. You see that in Ada, the main.adb had to "with" both Account 
  90. and Saving_account, while in C++, the main had to only include 
  91. "saving_account.h" .
  92.  
  93. thanks,
  94. Nasser
  95.  Learning_Ada95_OO_features...
  96.  
  97. ------------------ account.ads ---------------------------
  98. package Account is
  99.  type Money_Type is new integer range 1..100; -- just as an example
  100.  type Account_Type is tagged
  101.    record
  102.      Balance    : Money_Type;
  103.      Account_Id : Integer; 
  104.    end record;
  105. end Account;
  106.  
  107. ----------------- saving_account.ads ----------------
  108. with Account;
  109. Package Saving_Account is
  110.  type Saving_Account_Type is new Account.Account_Type with
  111.    record
  112.      Interest : Account.Money_Type;
  113.    end record;
  114.  
  115.    -- here we could also type this:
  116.    --
  117.    -- Type Money_Type is new Account.Money_Type
  118.    --
  119.    -- this way Money_Type can be made visible to clients
  120.    -- of saving account without having to "with" Account package.
  121.    -- we have propogated Money_Type to this package from the
  122.    -- "with"ed package, so that clients to this package can see
  123.    -- this type without having to "with" the Account package
  124.    -- JUST to access Money_Type
  125.    --
  126. end Saving_Account;
  127.  
  128. --------------- main.adb ---------------------------
  129.  
  130. with Account;   -- only reason with'ed is to have visibility to
  131.                 -- money_type. in C++, the main can see the Money_Type
  132.                 -- through Saving_Account, and do not have to include
  133.                 -- the base class Account
  134. with Saving_Account;
  135. with ada.text_io; use ada.text_io;
  136.  
  137. procedure main is       
  138.   package Money_IO is new Ada.Text_IO.Integer_IO(Account.Money_Type);
  139.   The_Saving_Account : Saving_Account.Saving_Account_Type;
  140.   The_Balance : Account.Money_Type; 
  141.   begin
  142.     Money_IO.Get(The_Balance);
  143.     The_Saving_Account.Balance := The_Balance;
  144. end main;
  145.  
  146. ==============================
  147.  
  148. Now the C++ way:
  149.  
  150.  
  151. --------------- account.h -------------------
  152. class Account
  153. {
  154.   public:
  155.   typedef int Money_Type;
  156.   Money_Type Balance;
  157.   int        Account_Id;
  158. };
  159.  
  160. -------------- saving_account.h -----------------
  161.  
  162. #include "saving_account.h" 
  163. class Saving_Account : public Account
  164. {
  165.   public:
  166.   Money_Type Interest;
  167. };
  168.  
  169. --------------- main.cpp -----------------
  170.  
  171. #include <iostream.h>
  172. #include "saving_account.h"  // Notice: no need to include to base class
  173.                              // Account in account.h
  174. main()
  175. {
  176. Saving_Account             The_Saving_Account;
  177. Saving_Account::Money_Type  The_Balance;  // notice, Money_Type accessed
  178.                                           // through Saving_Account and
  179.                                           // no need to include the
  180.                                           // base class account
  181.  
  182.  cout << "Balance?";
  183.  cin  >> The_Balance;
  184.  The_Saving_Account.Balance = The_Balance;
  185.  
  186. return 1;
  187.  
  188. }
  189.  
  190.  
  191.  
  192.